<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/16
 * Time: 14:06
 */
namespace app\index\Controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class Shop extends Common {
	
	/**
	 * 判断是否登录
	 */
	public function _initialize()
	{
		parent::_initialize();
	}
	/**
	 * 店铺首页
	 * Method Get
	 * @param string get.title 要检索的字段
	 * @return Array result 查询的结果
	 * @return string title 检索的字段
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('shop')->where('status',1)->order('id desc');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('linkman|telephone','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->paginate(8,false,$where);
		return view('index',compact('result'));
	}
	/**
	 * 店铺添加
	*/
	public function add()
	{
		return view('add');
	}
	/**
	 * 执行店铺添加
	 * Method Post
	 * @param string post.linkman 联系人
	 * @param string post.telephone 联系电话
	 * @param string post.address 地址
	 * @return json success 添加成功  error 添加失败
	*/
	public function insert()
	{
		if(Request()->isAjax()){
			try{
				Db::startTrans();
				$input['linkman'] = input('post.linkman');
				$input['telephone'] = input('post.telephone');
				$input['address'] = input('post.address');
				$result = Db::table('shop')->insertGetId($input);
				if(!$result){
					throw new \PDOException('添加失败');
				}
				Db::commit();
				$success = ['success' => '添加成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error' => $e->getMessage()];
				return json($error);
			}
			
		}
	}
	/**
	 * 修改店铺
	 * @param string get.id 店铺id
	 * @return Array result 查询结果
	*/
	public function edit()
	{
		$result = Db::table('shop')->where('id',input('get.id'))->find();
		return view('edit',compact('result'));
	}
	
	/**
	 * 执行店铺修改
	 * @param string post.id
	 * @param string post.linkman
	 * @param string post.telephone
	 * @param string post.address
	 * @return json success 修改成功 error 修改失败的错误信息
	*/
	public function update()
	{
		if(Request()->isAjax()){
			try{
				//开始事务
				Db::startTrans();
				$input['linkman'] = input('post.linkman');
				$input['telephone'] = input('post.telephone');
				$input['address'] = input('post.address');
				$id = input('post.id');
				$result = Db::table('shop')->where('id',$id)->update($input);
				if(!$result){
					throw new \PDOException('修改失败');
				}
				//事务提交
				Db::commit();
				$success = ['success'=>'修改成功'];
				return json($success);
			}catch (\PDOException $e){
				//事务回滚
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 *  删除店铺
	 * @param string post.id
	 * @return json success 删除成功 error 删除失败错误信息
	*/
	public function delete()
	{
		if(Request()->isAjax()){
			try{
				Db::startTrans();
				$result = Db::table('shop')->where('id',input('post.id'))->update(['status'=>2]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				Db::commit();
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 查看店铺订单
	 * Method Get
	 * @param string get.id 店铺id
	 * @param string get.title 要检索的字段
	 * @return string id 店铺id
	 * @return string title 检索的字段
	 * @return Array result 查询结果
	*/
	public function order()
	{
		$where = ['query'=>[]];
		$sid = trim(input('get.sid'));
		$where['query']['sid'] = $sid;
		$db = Db::table('purchase')
			->field('purchase.ordernumber,purchase.date,purchase.total,purchase.status,purchase.id,
					purchase.actualtotal,purchase.number,product.pname,product.retailprice,
					product.purchaseprice,business.linkman,purchase.pmethod')
			->join('shop','shop.id = purchase.sid','RIGHT')
			->join('product','product.id = purchase.pid','LEFT')
			->join('business','business.id = purchase.bid','LEFT')
			->where('shop.id',$sid)
			->whereNotIn('purchase.status',[0])
			->group('purchase.ordernumber')
			->order('purchase.id desc');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('product.pname|purchase.number|purchase.ordernumber|business.linkman','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->paginate(8,false,$where);
		return view('order',compact('result','title','sid'));
	}
	/**
	 * 订单详情
	 * @param string get.ordernumber 订单号
	 * @param string get.sid 店铺id
	 * @return Array result 查询结果
	*/
	public function detail()
	{
		$where = ['query'=>[]];
		$ordernumber = trim(input('get.ordernumber'));
		$sid = trim(input('get.sid'));
		$where['query']['ordernumber'] = $ordernumber;
		$where['query']['sid'] = $sid;
		$result = Db::table('purchase')
				->field('purchase.ordernumber,purchase.date,purchase.total,purchase.status,purchase.id,
						purchase.actualtotal,purchase.number,product.pname,product.retailprice,
						product.purchaseprice,business.linkman,purchase.pmethod')
				->join('shop','shop.id = purchase.sid','LEFT')
				->join('product','product.id = purchase.pid','LEFT')
				->join('business','business.id = purchase.bid','LEFT')
				->where('purchase.ordernumber',$ordernumber)
				->where('purchase.status','<>',0)
				->where('shop.id',$sid)
				->paginate(8,false,$where);

		return view('detail',compact('result','sid','ordernumber'));
	}
	/**
	 * 订单详情
	 * @param string get.id 订单id
	 * @param string get.sid 店铺id
	*/
	public function orderDetail()
	{
		$id = trim(input('get.id'));
		$sid = trim(input('get.sid'));
		$result = Db::table('purchase')
				->join('shop','shop.id = purchase.sid','LEFT')
				->join('product','product.id = purchase.pid','LEFT')
				->join('business','business.id = purchase.bid','LEFT')
				->where('purchase.id',$id)
				->where('shop.id',$sid)
				->find();
		$result['image'] = json_decode($result['image']);
		return view('orderDetail',compact('result'));
	}
	/**
	 * 产品损坏
	 * @param string get.id 订单号
	*/
	public function damage()
	{
		$id = trim(input('get.id'));
		return view('damage',compact('id'));
	}
	
	/**
	 * 执行损坏产品添加
	 * @param string get.id 订单号
	 * @param string get.number 订单号
	*/
	public function doDamage()
	{
		if(Request()->isAjax()){
			try{
				Db::startTrans();
				$input['pid'] = input('post.pid');
				$input['number'] = input('post.number');
				$value = Db::table('damage')->where('pid',$input['pid'])->find();
				if($value){
					throw new \PDOException('损坏个数已添加，请去修改，不能重复前添加');
				}
				$result = Db::table('damage')->insertGetId($input);
				if(!$result){
					throw new \PDOException('添加失败');
				}
				Db::commit();
				$success = ['success' => '添加成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 损坏个数
	 * @param string get.id 店铺id
	 * @param string get.title 检索的字段
	 * @return Array result 查询结果
	*/
	public function damagenum()
	{
		$where = ['query'=>[]];
		$id = input('get.id');
		$where['query']['id'] = input('get.id');
		$db = Db::table('purchase')
			->field('purchase.number,damage.number as num,damage.id as did,product.pname,
					purchase.date,purchase.ordernumber,purchase.id')
			->join('damage','damage.pid = purchase.id','RIGHT')
			->join('product','product.id = purchase.pid','LEFT')
			->join('shop','shop.id = purchase.sid','LEFT')
			->order('purchase.id desc')
			->where('shop.id',input('get.id'));
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('purchase.ordernumber|product.pname','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->paginate(8,false,$where);
		return view('damagenum',compact('result','id','title'));
		
	}
	
	/**
	 * 修改损坏数量
	 * @param string get.did 损坏id
	 * @param string get.number 损坏数量
	 * @return json success 修改成功 error 修改失败错误信息
	*/
	public function updateNumber()
	{
		if(Request()->isAjax()){
			try{
				Db::startTrans();
				$result = Db::table('damage')->where('id',input('post.did'))->update(['number'=>input('post.number')]);
				if(!$result){
					throw new \PDOException('修改失败');
				}
				Db::commit();
				$success = ['success' => '修改成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 查看库存
	 * @param string get.id 商家id
	 * @param string get.title 要检索的字段
	 * @return Array result 查询结果
	*/
	public function inventory()
	{
		$where = ['query'=>[]];
		$id = input('get.id');
		$where['query']['id'] = $id;
		$db = Db::table('inventory')
			->field('product.pname,inventory.number,inventory.id')
			->join('product','inventory.pid = product.id','LEFT')
			->join('shop','inventory.sid = shop.id','LEFT')
			->where('shop.id',input('get.id'));
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('product.pname','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->order('inventory.id desc')->paginate(8,false,$where);
		return view('inventory',compact('result','id','title'));
	}
	
	
	/**
	 * 后付款订单
	 * @param string get.sid 店id
	 * @param string get.ordernumber 订单号
	*/
	public function postpayment()
	{
		$where = ['query'=>[]];
		$sid = input('get.sid');
		$where['query']['sid'] = $sid;
		$db = Db::table('postpayment')->field('postpayment.ordernumber,postpayment.date')
				->join('shop','shop.id = postpayment.sid','RIGHT')
				->where('shop.id',$sid)
				->where('postpayment.status','<>',0)
				->order('postpayment.id desc')
				->group('ordernumber');
		if(!empty(trim(input('get.ordernumber')))){
			$ordernumber = trim(input('get.ordernumber'));
			$db->where('ordernumber',$ordernumber);
			$where['query']['ordernumber'] = $ordernumber;
		}
		$result = $db->paginate(8,false,$where);
		return view('postpayment',compact('result','sid','ordernumber'));
	}
	
	/**
	 * 详情
	 * @param string get.sid 店id
	 * @param string get.ordernumber 订单号
	*/
	public function postdetail()
	{
		$sid = input('get.sid');
		$ordernumber = input('get.ordernumber');
		$result = Db::table('postpayment')
				->field('product.pname,product.purchaseprice,product.retailprice,business.linkman,
						postpayment.total,postpayment.actualtotal,postpayment.number,postpayment.date,
						postpayment.ordernumber,postpayment.status,postpayment.id')
				->join('shop','shop.id = postpayment.sid','RIGHT')
				->join('product','product.id = postpayment.pid','LEFT')
				->join('business','business.id = postpayment.bid','LEFT')
				->where('shop.id',$sid)
				->where('postpayment.ordernumber',$ordernumber)
				->where('postpayment.status','<>',0)
				->order('postpayment.id desc')
				->paginate(8);
		return view('postdetail',compact('result','sid'));
	}
	
	/**
	 * 损坏数量
	 * @param string get.id 订单id
	*/
	public function postdamage()
	{
		$id = input('get.id');
		return view('postdamage',compact('id'));
	}
	
	/**
	 *添加损坏个数
	 *@param string post.id 订单号
	 *@param string post.num 损坏数量
	*/
	public function postdamagenum()
	{
		if(request()->isAjax()){
			try{
				$postid = input('post.postid');
				$num = input('post.num');
				$result = Db::table('damage')->insertGetId(['postid'=>$postid,'number'=>$num]);
				if(!$result){
					throw new \PDOException('添加失败');
				}
				$success = ['success'=>'添加成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 订单详情
	 * @param string post.sid 店id
	 * @param string post.ordernumber 订单号
	*/
	public function postorder()
	{
		$sid = input('get.sid');
		$id = input('get.id');
		$result = Db::table('postpayment')
			->join('shop','shop.id = postpayment.sid','LEFT')
			->join('product','product.id = postpayment.pid','LEFT')
			->join('business','business.id = postpayment.bid','LEFT')
			->where('postpayment.id',$id)
			->where('postpayment.sid',$sid)
			->find();
		$result['image'] = json_decode($result['image']);
		return view('postorder',compact('result'));
	}
	
}